package com.asiainfo.iyoga.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.asiainfo.iyoga.DriverManager;
import com.asiainfo.iyoga.model.Member;


public class MemberDao {
	
	Connection conn = null;
	
	public MemberDao() {
		DriverManager manager = new DriverManager();
		conn = manager.getConnection();
	}
	
	public boolean addMember(Member member) {
		//
		PreparedStatement pst = null; 
    	String sql = "insert into member(NAME,ADDRESS,JOB) values('"+
    			member.strName+"','" + member.strAddress +"','" + member.strJob+"')";
    	try {
    		pst = conn.prepareStatement(sql);
    		pst.execute(sql);
    	} catch (Exception e) {
    		e.printStackTrace();
    		return false;
    	}     	
    	return true;
	}
	
	
	public int getIdByName(String name) {
		int i = 0;
		String sql = "select MEMBER_ID from member where NAME = ?";
		PreparedStatement pstmt;
		try {
	        pstmt = (PreparedStatement) conn.prepareStatement(sql);
	        pstmt.setString(1, name);
	        ResultSet rs = pstmt.executeQuery();
	        if(rs.next()) {
	        	i = rs.getInt("MEMBER_ID");
	        }
	        pstmt.close();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
		return i;
	}
	
	
	public void close() {
		try {
			conn.close();
		} catch(SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Member mem = new Member("cyj1","js1","coder1");
		MemberDao memberDao = new MemberDao();
		memberDao.addMember(mem);
	}
}
